package net.airtheva.uniqueTask4;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.util.Log;

public class Utils {

	public static final String TAG = "Utils";
	
	public static final Pattern PATTERN_AUDIO = Pattern.compile("^.+\\.(mp3|json)$");
	
	public static File DataDir;
	public static File PlaylistsDir;
	
	public static void Initialize(Context context) {
		
		DataDir = context.getFilesDir();
		if(!DataDir.exists()) {
			DataDir.mkdir();
		}
		
		PlaylistsDir = new File(DataDir, "./playlists/");
		if(!PlaylistsDir.exists()) {
			PlaylistsDir.mkdir();
		}
		
		
		
		Log.d(TAG, PlaylistsDir.exists() + "");
		
	}
	
	public static int FindArea(Long[] area, Long value) {
		
		int leftIndex = 0;
		int rightIndex = area.length - 1;
		
		while(true) {
			int middleIndex = (int) Math.floor((leftIndex + rightIndex) / 2);
			int indexDelta = rightIndex - leftIndex;
			
			if(indexDelta == 1) {
				break;
			}
			else if(area[middleIndex] > value) {
				rightIndex = middleIndex;
			}
			else if(area[middleIndex] <= value){
				leftIndex = middleIndex;
			}
			else {
				return -1;
			}
		}
		
		return leftIndex;
		
	}
	
	public static String Basename(String filename) {
		
		String[] parts = filename.split("\\.");
		
		return parts[0];
		
	}
	
	public static String Join(Object[] array, String join) {
		
		StringBuilder sb = new StringBuilder();
		
		sb.append(array[0]);
		
		for(int i = 1; i < array.length; i++) {
			sb.append(join);
			sb.append(array[i]);
		}
		
		return sb.toString();
		
	}
	
	public static boolean IsAudioFilename(String filename) {
		
		Matcher m = PATTERN_AUDIO.matcher(filename);
		return m.matches();
		
	}
	
	public static File MakePlaylistFile(String name) {
		return new File(PlaylistsDir, name + ".json");
	}
	
	public static void WriteFile(File file, String content) {
		
		try {
			
			FileWriter fw = new FileWriter(file);
			fw.write(content);
			fw.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static String ReaderToString(BufferedReader br) {
		
		try {
			
			StringBuilder sb = new StringBuilder();
			
			while(true) {
				
				String line = br.readLine();
				if(line == null) {
					break;
				}
				sb.append(line);
				sb.append("\n");
				
			}
			
			return sb.toString();
			
		} catch(Exception e) {
			
			e.printStackTrace();
			return null;
			
		}
		
	}
	
	public static String ReadFile(File file) {
		
		try {
			
			BufferedReader br = new BufferedReader(new FileReader(file));
			
			String s = ReaderToString(br);
			
			br.close();
			
			return s;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return null;
		}
		
	}
	
	public static MainActivity GetMainActivity(Fragment fragment) {
		
		MainActivity mainActivity = (MainActivity) fragment.getActivity();
		return mainActivity;
		
	}
	
	public static PlayerService GetPlayerService(Fragment fragment) {
		
		PlayerService playerService = GetMainActivity(fragment).Service;
		return playerService;
	}
	
}
